/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ca.barrenechea.stickyheaders.adapter;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSliderProvider;
import ohos.app.Context;

import java.util.List;

import ca.barrenechea.stickyheaders.ResourceTable;
import ca.barrenechea.stickyheaders.page.BasePage;
import ca.barrenechea.stickyheaders.page.DoubleHeaderPage;
import ca.barrenechea.stickyheaders.page.InlineDoubleHeaderPage;
import ca.barrenechea.stickyheaders.page.InlineStickyHeaderPage;
import ca.barrenechea.stickyheaders.page.StickyHeaderPage;

/**
 * page适配器
 *
 * @since 2021-04-13
 */
public class TabPageAdapter extends PageSliderProvider {
    private static final int STICKY_HEADER_PAGE = 0;
    private static final int INLINE_STICKY_HEADER_PAGE = 1;
    private static final int DOUBLE_HEADER_PAGE = 2;
    private static final int INLINE_DOUBLE_HEADER_PAGE = 3;
    private List<String> data;
    private Context context;
    private BasePage tabPage;

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param data 数据源
     */
    public TabPageAdapter(Context context, List<String> data) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int position) {
        ComponentContainer layout = initTabPage(position);
        if (layout != null) {
            tabPage.init(layout);
            componentContainer.addComponent(layout);
        }
        return layout;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int position, Object object) {
        componentContainer.removeComponent((Component) object);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object object) {
        return component == object;
    }

    private ComponentContainer initTabPage(int tabPositon) {
        ComponentContainer layout = null;
        switch (tabPositon) {
            case STICKY_HEADER_PAGE:
                layout = (ComponentContainer) LayoutScatter.getInstance(context).parse(
                    ResourceTable.Layout_layout_sticky, null, false);
                tabPage = new StickyHeaderPage(context);
                break;
            case INLINE_STICKY_HEADER_PAGE:
                layout = (ComponentContainer) LayoutScatter.getInstance(context).parse(
                    ResourceTable.Layout_layout_sticky_inline, null, false);
                tabPage = new InlineStickyHeaderPage(context);
                break;
            case DOUBLE_HEADER_PAGE:
                layout = (ComponentContainer) LayoutScatter.getInstance(context).parse(
                    ResourceTable.Layout_layout_double, null, false);
                tabPage = new DoubleHeaderPage(context);
                break;
            case INLINE_DOUBLE_HEADER_PAGE:
                layout = (ComponentContainer) LayoutScatter.getInstance(context).parse(
                    ResourceTable.Layout_layout_double_inline, null, false);
                tabPage = new InlineDoubleHeaderPage(context);
                break;
            default:
                return null;
        }
        return layout;
    }
}
